package main

import (
	"fmt"
	"sort"
)

func main() {

	nums := []int{2, 9, 1, 2, 3}
	/**
	基本类型的自定义排序：奇数在前，偶数在后
	使用 sort.Slice(nums, func{...})
	*/
	sort.Slice(nums, func(i, j int) bool {
		if nums[i]%2 == 1 {
			return true
		} else {
			if nums[j]%2 == 0 {
				return true
			} else {
				return false
			}
		}
	})
	fmt.Println(nums) //[3 1 9 2 2]

	user := []struct {
		Name string
		Age  int
	}{
		{"kelvin", 20},
		{"allen", 24},
		{"stephen", 24},
		{"dfsf", 12},
		{"leo", 24},
	}

	//sort.SliceStable(user, func(i, j int) bool {
	//	return user[i].Age > user[j].Age
	//})
	sort.Slice(user, func(i, j int) bool {
		return user[i].Age > user[j].Age
	})
	fmt.Println(user) // 输出[{allen 24} {stephen 24} {leo 24} {kelvin 20} {dfsf 12}]

}
